import { t } from 'i18next'
import { useEffect, useRef } from 'react'
import { PollRef } from './VmpPoll'
import { PlanRef } from './VmpPlan'
import { Alert } from '@/components/Alert'
import { useTvWallStore } from './store'
import { useMccStore } from '../../store'
import { Message } from '@kedacom/react-next'
import shallow from 'zustand/shallow'
import { fetchChnageStyleInfo } from '@/api/manager/tvwall'
import { revert_poll } from '@/api/manager/poll/interface'

export default function useService() {
	const { meetingMoid } = useMccStore(
		(state) => ({
			meetingType: state.mccInfo.meetingType,
			meetingMoid: state.mccInfo.meetingMoid,
		}),
		shallow
	)
	const { isOpen, update, loadAllStyle, show_mt_name, correct_style, getMccTVWallsByMeetingMoid, onInit } =
		useTvWallStore((state) => ({
			isOpen: state.isOpen,
			data: state.data,
			updateData: state.updateData,
			update: state.update,
			loadAllStyle: state.loadAllStyle,
			correct_style: state.correct_style,
			show_mt_name: state.show_mt_name,
			getMccTVWallsByMeetingMoid: state.getMccTVWallsByMeetingMoid,
			onInit: state.onInit,
		}))
	// const [isStart, setIsStart] = useState(false) //当前是否开启画面合成
	// const [isEdit, setIsEdit] = useState(false) //当前是否处于编辑状态
	// const disabled = useMemo(() => isStart && !isEdit, [isStart, isEdit])
	const pollRef = useRef<PollRef | null>(null)
	const planRef = useRef<PlanRef | null>(null)
	useEffect(() => {
		// loadMaskRef.current = Message.loading('正在加载画面合成信息，请稍候……')
		if (isOpen) {
			onInit()
			loadAllStyle()
		}
	}, [isOpen])
	const handleStartbatchPoll = async () => {
		const _flag = pollRef.current?.testValue()
		if (_flag) {
			let _batch_poll_params = pollRef.current?.getValue()
			if (!_batch_poll_params) return
			const mask = Message.loading(t('正在处理，请稍候……'))
			const params = {
				mode: 2,
				show_mt_name: show_mt_name,
				batch_poll: Object.assign(revert_poll(_batch_poll_params), { state: 1 }),
				members: [],
			}
			const res = await fetchChnageStyleInfo(meetingMoid, correct_style, params)
			if (res.success) {
				update({ is_batch_poll: true })
				setTimeout(() => {
					mask.hide()
				}, 2000)
			} else {
				mask.hide()
				Alert(res.description || '', function () {
					getMccTVWallsByMeetingMoid(meetingMoid)
				})
			}
		}
	}
	const handleModifyApply = async () => {
		const _flag = pollRef.current?.testValue()
		if (_flag) {
			let _batch_poll_params = pollRef.current?.getValue()
			if (!_batch_poll_params) return
			const mask = Message.loading(t('正在处理，请稍候……'))
			const params = {
				mode: 2,
				show_mt_name: show_mt_name,
				batch_poll: Object.assign(revert_poll(_batch_poll_params), { state: 1 }),
				members: [],
			}
			const res = await fetchChnageStyleInfo(meetingMoid, correct_style, params)
			if (res.success) {
				update({ is_editing: false })
				setTimeout(() => {
					mask.hide()
				}, 2000)
			} else {
				mask.hide()
				Alert(res.description || '', function () {
					getMccTVWallsByMeetingMoid(meetingMoid)
				})
			}
		}
	}
	return {
		pollRef,
		planRef,
		handleStartbatchPoll,
		handleModifyApply,
	}
}
